package model.comparisons;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import model.IComparison;

public class Comparison3 implements IComparison {

	private List<Integer> arraylist = new ArrayList<Integer>();
	private boolean isactive;
	private float[] times = new float[2];
	private int N;

	@Override
	public void setN(int N) {
		this.N = N;

	}

	@Override
	public void setActive(boolean isactive) {
		this.isactive = isactive;
	}

	@Override
	public float[] getTime() {
		if (isactive) {

			int[] x = new int[N];
			for (int i = 0; i < N; i++) {
				x[i] = (int) (Math.random() * 100);
			}

			for (int i = 0; i < N; i++) {
				arraylist.add(i, x[i]);
			}
			// primo
			float startTime = System.nanoTime();
			Collections.sort(arraylist);

			times[0] = ((float) (System.nanoTime() - startTime)) / 1000000;
			// secondo
			float startTime2 = System.nanoTime();

			arraylist.clear();

			for (int i = 0; i < N; i++) {
				arraylist.add(i, x[i]);
			}

			for (int i = 0; i < arraylist.size(); i++) {
				for (int j = 0; j < arraylist.size() - 1; j++) {

					if (arraylist.get(j) > arraylist.get(j + 1)) {
						int k = arraylist.get(j);
						arraylist.set(j, arraylist.get(j + 1));
						arraylist.set(j + 1, k);
					}
				}
			}

			times[1] = ((float) (System.nanoTime() - startTime2)) / 1000000;;

			//System.out.println(times[0] + " " + times[1]);// test

			return times;
		}

		return new float[] { 1, 1 };
	}

}
